/*
 * Copyright (c) 2021 FuZhou LOCKZHINER Electronic Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
#ifndef __E53_TEMPERATURE_MEASUREMENT_H__
#define __E53_TEMPERATURE_MEASUREMENT_H__

#include "lz_hardware.h"
#include "los_task.h"

#define FAIL        1
#define SUCCESS     0

typedef struct
{
    float temperature;
    int flag;
} e53_tm_data_t;

#define TM_FIFO_MAXSIZE         10 // 队列缓存数

/* 定义先进先出队列  缓存原始数据*/
typedef struct
{
    UINT32    sem;
    uint16_t  offset_read;
    uint16_t  offset_write;
    e53_tm_data_t buffer[TM_FIFO_MAXSIZE];
} tmfifo_t;

static inline void FifoInitTm(tmfifo_t *fifo)
{
    fifo->offset_read = fifo->offset_write = 0;
    if (LOS_SemCreate(1, &fifo->sem) != 0)
    {
        printf("Falied to create Semaphore\n");
    }
}

static inline void FifoPutTm(tmfifo_t *fifo, e53_tm_data_t *hrd)
{
    if (LOS_SemPend(fifo->sem, 2) == 0)
    {
        memcpy(&fifo->buffer[fifo->offset_write], hrd, sizeof(e53_tm_data_t));
        fifo->offset_write = (fifo->offset_write + 1) % TM_FIFO_MAXSIZE;
        LOS_SemPost(fifo->sem);
    }
}

static inline uint16_t FifoGetTm(tmfifo_t *fifo, e53_tm_data_t *hrd)
{
    if (LOS_SemPend(fifo->sem, 2) == 0)
    {
        if (fifo->offset_read == fifo->offset_write)
        {
            LOS_SemPost(fifo->sem);
            return 0;
        }
        
        memcpy(hrd, &fifo->buffer[fifo->offset_read], sizeof(e53_tm_data_t));
        fifo->offset_read = (fifo->offset_read + 1) % TM_FIFO_MAXSIZE;
        LOS_SemPost(fifo->sem);
        return 1;
    }
    return 0;
}


unsigned int e53_tm_read_data(float *temperature, float *f);
// void tm_light_set_status(SWITCH_STATUS_ENUM status);
extern tmfifo_t tmfifo;
extern e53_tm_data_t g_tm_data;
#endif/*__E53_TEMPERATURE_MEASUREMENT_H__*/

